//
//  Actions.m
//  <4>Speaker
//
//  Created by chen on 16/9/7.
//  Copyright © 2016年 tyrad. All rights reserved.
//

#import "Actions.h"

@interface Actions ()<NSApplicationDelegate,NSSpeechSynthesizerDelegate ,NSTableViewDelegate , NSTableViewDataSource>
{
    NSSpeechSynthesizer *_speechSythesizer;
}
@end

@implementation Actions

- (instancetype)init
{
    self = [super init];
    if (self) {
        _speechSythesizer = [[NSSpeechSynthesizer alloc]initWithVoice:nil];
        _speechSythesizer.delegate = self;
        _dataArray = [NSSpeechSynthesizer availableVoices];
        
    }
    return self;
}

- (void)awakeFromNib{
    
    [_textField setPlaceholderString:@"请输入文本"];
    [_textField setStringValue:@"嘿嘿"];
 
    [_stopButton setTarget:self];
    [_stopButton setAction:@selector(stopClick:)];
  
    //用代码的方式写action
    SEL selector = @selector(speakClick:);
    [_speakButton setTarget:self] ;
    [_speakButton setAction:selector];
    
    //如运行时用 NSString 找到选择器,那么可以 `NSSelectorFromString`
    //SEL mySelector = NSSelectorFromString(@"speakClick:")
    //界面刚出来时
    _stopButton.enabled = NO ;
    
    
    //刚开始时,默认选中声音
    
    NSString *defaultVoice = [NSSpeechSynthesizer defaultVoice];
    NSInteger idnex = [_dataArray indexOfObject:defaultVoice];
    NSIndexSet *index = [NSIndexSet indexSetWithIndex:idnex];

    [_myTableView selectRowIndexes:index byExtendingSelection:NO];
    [_myTableView scrollRowToVisible:idnex];
    
}


- (void)stopClick:(id)p{
    
    [_speechSythesizer stopSpeaking];
    
}

- (void)speakClick:(id)p{
    
    NSString *voice = self.textField.stringValue ; 
     
    if (voice.length > 0 ) {
        [_speechSythesizer startSpeakingString:voice];
        
        _stopButton.enabled = YES ;
        _speakButton.enabled = NO ;
        _myTableView.enabled = NO;
    }
}


#pragma mark -- speechSynthesizer Delegate
- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking{
    NSLog(@"完成播放 %@", @(finishedSpeaking)) ;
    
    _stopButton.enabled = NO ;
    _speakButton.enabled = YES ;
    _myTableView.enabled = YES ;
}

- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender willSpeakWord:(NSRange)characterRange ofString:(NSString *)string{
    NSLog(@"willSpeakWord") ;

}
- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender willSpeakPhoneme:(short)phonemeOpcode{
    NSLog(@"willSpeakPhoneme") ;

}


#pragma mark -- TableView  DataSource

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{

    return self.dataArray.count ;
}

- (nullable id)tableView:(NSTableView *)tableView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row{
  
    //tableView类型为 `cellBase` ， 这时能正常显示字符串数据
    
    NSString *stringValue = [self.dataArray objectAtIndex:row] ;
    
    return stringValue ;
}

#pragma mark -- TableView Delegate

- (void)tableViewSelectionDidChange:(NSNotification *)notification{
    
    NSLog(@"NSTabelView =  %@", notification.object);
    
    NSInteger row = [_myTableView selectedRow];
    
    NSString *selectedString  = self.dataArray[row];
    
    [_speechSythesizer setVoice:selectedString];
    
}



@end













